package Vistas;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import A.Datos.ConexionMantenimiento;
import javax.swing.JDesktopPane;
import java.awt.Component;
import javax.swing.JScrollPane;

public class VerApuntes extends JInternalFrame implements ActionListener {

	private JPanel contentPane;
	static Connection conn = null;
	static ResultSet rs = null;
	static Statement st = null;
	static String tablaBD = "Apuntes";
	private String atributos[] = {"id_apunte","desc_apuntes","id_profesor"};
	private JDesktopPane desktopPane;
	DefaultTableModel dtm = new DefaultTableModel();
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VerApuntes frame = new VerApuntes();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public VerApuntes() {
		
		setMaximizable(true);
		setClosable(true);
		dtm.setColumnIdentifiers(atributos);
		setBounds(100, 100, 736, 487);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		
		JButton btnMostrar = new JButton("Mostrar Apuntes");
		btnMostrar.setName("Muestro");
		btnMostrar.addActionListener(this);
		
		JButton btnNuevoApunte = new JButton("Nuevo Apunte");
		btnNuevoApunte.setName("Nuevo");
		btnNuevoApunte.addActionListener(this);
		
		desktopPane = new JDesktopPane();
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnMostrar)
							.addPreferredGap(ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
							.addComponent(btnNuevoApunte))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
							.addGap(2)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
					.addGap(15)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNuevoApunte)
						.addComponent(btnMostrar))
					.addContainerGap())
		);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 668, 366);
		desktopPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
			},
			new String[] {
				"Numero Apunte", "Descripcion"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton boton = (JButton) e.getSource();
		if(boton.getName().equals("Muestro")){
			table.setModel(dtm);
			try {
	            int i, f;
	            conn = ConexionMantenimiento.Enlace(conn);
	            rs = ConexionMantenimiento.rset(rs,tablaBD);
	            String datos[] = new String[3];
	            f = dtm.getRowCount();
	            if (f > 0) 
	                for (i = 0; i < f; i++)
	                    dtm.removeRow(0);
	                
	            while (rs.next()) {
	                datos[0] = (String) rs.getString(1);
	                datos[1] = (String) rs.getString(2);
	                datos[2] = (String) rs.getString(3);
	                dtm.addRow(datos);
	            }
	            }
	        catch(SQLException ev){
	            JOptionPane.showMessageDialog(null, "Error en bd "+ev.toString());
	        }
		}else{
			Apuntes apuntes = new Apuntes();
			desktopPane.removeAll();
			desktopPane.add(apuntes);
			apuntes.setVisible(true);	
		}
	}
}
